package com.cloudkinto.appletController.order;


import com.cloudkinto.appletservice.order.AppletOrderAmazonService;
import com.cloudkinto.appletservice.order.vo.AppletExport2ShipOrderReq;
import com.cloudkinto.appletservice.order.vo.amazon.AppletAmazonOrderDetailRes;
import com.cloudkinto.appletservice.order.vo.amazon.AppletAmazonOrderPageRes;
import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.order.vo.amazon.UpdateSysMemoReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 张永远
 * @since 2020-06-02
 */
@RestController
@Api(tags = "小程序亚马逊订单模块")
@RequestMapping("/api/applet/orderAmazon")
public class AppletOrderAmazonController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(AppletOrderAmazonController.class);

    @Autowired
    private AppletOrderAmazonService service;

    @UserSessionRequired
    @GetMapping("/downloadInit")
    @ApiOperation(value = "店铺初始化", notes = "初始化")
    public SingleResult addInit() {
        return service.addInit(getCurrentUserId(), getCompanyId());
    }

//    @UserSessionRequired
//    @PostMapping("downloadAmazonOrders")
//    @ApiOperation(value = "下载亚马逊订单", notes = "下载亚马逊订单")
//    public SingleResult add(@RequestBody @Validated AmazonOrderDownReq req) throws Exception {
//        Long userId = getCurrentUserId();
//        Long companyId = getCompanyId();
//        return service.downloadAmazonOrders(req, userId, companyId);
//    }


    @UserSessionRequired
    @PostMapping("mark/sendOut")
    @ApiOperation(value = "标记为发货", notes = "标记为发货")
    public SingleResult markSendOut(@RequestBody Long id) throws Exception {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        List<Long> ids = new ArrayList<>();
        ids.add(id);
        return service.markSendOut(ids, userId);
    }


    @UserSessionRequired
    @PostMapping("sendOut")
    @ApiOperation(value = "发货", notes = "发货")
    public SingleResult sendOut(@RequestBody AppletExport2ShipOrderReq req) throws Exception {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        if (req.getIsSolo() == 0) {
            return service.sendOut(req.getOrderid(), userId, companyId, true);
        } else {
            return service.sendOutNopackage(req.getOrderid(), userId, companyId);
        }
    }

//    @UserSessionRequired
//    @PostMapping("sendOutMatch")
//    @ApiOperation(value = "确认发货发货", notes = "确认发货发货")
//    public SingleResult sendOutMatch(@RequestBody List<OrderOutputAddReq> orderOutputAddReqList) throws Exception {
//        Long userId = getCurrentUserId();
//        return service.sendOutMatch(orderOutputAddReqList, userId);
//    }
//

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
    @ApiResponses({@ApiResponse(code = 200, message = "", response = AppletAmazonOrderDetailRes.class)})
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", service.detail(id));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return service.pageInit();
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiResponses({@ApiResponse(code = 200, message = "", response = AppletAmazonOrderPageRes.class)})
    public PageResult listPage(HttpServletRequest request) {
        Long companyId = getCompanyId();

        Map<String, String> map = new HashMap<>();
        String keyword = request.getParameter("keyword");
//        String shopId = request.getParameter("shopId");
//        String startTime = request.getParameter("startTime");
//        String endTime = request.getParameter("endTime");
//        String orderStatus = request.getParameter("orderStatus");
//        String fulfillmentChannel = request.getParameter("fulfillmentChannel");
//        String isSendOut = request.getParameter("isSend");

        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");

        map.put("companyId", String.valueOf(companyId));
        map.put("keyword", keyword);
//        map.put("startTime", startTime);
//        map.put("endTime", endTime);
//        if (!StringUtils.isEmpty(shopId)) {
//            map.put("shopId", shopId);
//        } else if (getShopId() != null) {
//            map.put("shopId", getShopId() + "");
//        }
//        map.put("orderStatus", orderStatus);
//        map.put("fulfillmentChannel", fulfillmentChannel);
//        map.put("isSendOut", isSendOut);
        map.put("current", current);
        map.put("pageSize", size);
        return service.getListPage(map);
    }

    @UserSessionRequired
    @PostMapping("systemMemo")
    @ApiOperation(value = "编辑系统备注", notes = "")
    public SingleResult systemMemo(@RequestBody @Validated UpdateSysMemoReq req) throws Exception {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        return service.updateSysMemo(req, userId, companyId);
    }

    @UserSessionRequired
    @GetMapping("systemMemoInit")
    @ApiOperation(value = "编辑系统备注初始化", notes = "")
    public SingleResult systemMemoInit(@RequestParam Long id) throws Exception {
        return service.systemMemoInit(id);
    }
}

